package com.charger.service.impl;

import java.text.ParseException;
import java.util.*;

import com.charger.common.utils.DateUtils;
import com.charger.domain.ChargerEnergyMiddle;
import com.charger.domain.ChargerEnergyRes;
import com.charger.utils.QDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.charger.mapper.ChargerEnergyMapper;
import com.charger.domain.ChargerEnergy;
import com.charger.service.IChargerEnergyService;

/**
 * 能耗分析Service业务层处理
 * 
 * @author qkp
 * @date 2024-11-22
 */
@Service
public class ChargerEnergyServiceImpl implements IChargerEnergyService 
{
    @Autowired
    private ChargerEnergyMapper chargerEnergyMapper;

    /**
     * 查询能耗分析
     * 
     * @param energyId 能耗分析主键
     * @return 能耗分析
     */
    @Override
    public ChargerEnergy selectChargerEnergyByEnergyId(Long energyId)
    {
        return chargerEnergyMapper.selectChargerEnergyByEnergyId(energyId);
    }

    /**
     * 查询能耗分析列表
     * 
     * @param chargerEnergy 能耗分析
     * @return 能耗分析
     */
    @Override
    public List<ChargerEnergy> selectChargerEnergyList(ChargerEnergy chargerEnergy)
    {
        return chargerEnergyMapper.selectChargerEnergyList(chargerEnergy);
    }

    /**
     * 新增能耗分析
     * 
     * @param chargerEnergy 能耗分析
     * @return 结果
     */
    @Override
    public int insertChargerEnergy(ChargerEnergy chargerEnergy)
    {
        chargerEnergy.setCreateTime(DateUtils.getNowDate());
        return chargerEnergyMapper.insertChargerEnergy(chargerEnergy);
    }

    /**
     * 修改能耗分析
     * 
     * @param chargerEnergy 能耗分析
     * @return 结果
     */
    @Override
    public int updateChargerEnergy(ChargerEnergy chargerEnergy)
    {
        return chargerEnergyMapper.updateChargerEnergy(chargerEnergy);
    }

    /**
     * 批量删除能耗分析
     * 
     * @param energyIds 需要删除的能耗分析主键
     * @return 结果
     */
    @Override
    public int deleteChargerEnergyByEnergyIds(Long[] energyIds)
    {
        return chargerEnergyMapper.deleteChargerEnergyByEnergyIds(energyIds);
    }

    /**
     * 删除能耗分析信息
     * 
     * @param energyId 能耗分析主键
     * @return 结果
     */
    @Override
    public int deleteChargerEnergyByEnergyId(Long energyId)
    {
        return chargerEnergyMapper.deleteChargerEnergyByEnergyId(energyId);
    }

    @Override
    public Map<String, Object> getList(Map params) throws ParseException {
        Map<String, Object> result = new HashMap<>();
        List<ChargerEnergyRes> list = new ArrayList<>();
        List<String> weeks = new ArrayList<>();
        String startTime = (String) params.get("startTime");
        List<String> searchTimes = new ArrayList<>();
        if (startTime != null && !"".equals(startTime)){
            String endTime = (String) params.get("endTime");
            Date startD = DateUtils.parseDate(startTime, "yyyy-MM-dd");
            Date endD = DateUtils.parseDate(endTime, "yyyy-MM-dd");
            long diff = (endD.getTime() - startD.getTime()) / (1000 * 3600 * 24);

            if (diff > 7){
                searchTimes = QDateUtils.getBefore(7, endD);
            }else{
                searchTimes = QDateUtils.getBefore((int) (diff + 1), endD);
            }

//            List<ChargerEnergyMiddle> chargerEnergyMiddles = chargerEnergyMapper.selectByTime(params);
        }else{
            Date endD = new Date();
            searchTimes = QDateUtils.getBefore(7 , endD);
        }
        Map param = new HashMap();
        Object chargeId = params.get("chargeId");
        Object stationId = params.get("stationId");
        if (chargeId != null){
            param.put("chargeId", Integer.parseInt((String) chargeId));
        }
        if (stationId != null && chargeId == null){
            List<Integer> idsByStationId = chargerEnergyMapper.getIdsByStationId(Integer.parseInt((String) stationId));
            if (idsByStationId != null && idsByStationId.size() > 0){
                param.put("chargeIds", idsByStationId);
            }else{
                result.put("list", new ArrayList<>());
                result.put("weeks", new ArrayList<>());
                result.put("times", new ArrayList<>());
                return result;
            }
        }
        for (int m = 0; m < searchTimes.size(); m++) {
            param.put("day", searchTimes.get(m));
            List<ChargerEnergyMiddle> chargerEnergyMiddles = chargerEnergyMapper.selectByTime(param);
//            System.out.println(Arrays.toString(chargerEnergyMiddles.toArray()));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateUtils.parseDate(searchTimes.get(m), "yyyy-MM-dd"));
            List<String> times = QDateUtils.getTime();
            ChargerEnergyRes chargerEnergyRes = new ChargerEnergyRes();
            out:for (int i = 0; i < times.size(); i++) {
                for (int j = 0; j < chargerEnergyMiddles.size(); j++) {
                    if (times.get(i).equals(chargerEnergyMiddles.get(j).getTime())){
                        chargerEnergyRes.getEnergys().add(chargerEnergyMiddles.get(j).getEnergy());
                        continue out;
                    }
                }
                chargerEnergyRes.getEnergys().add(0d);
            }
            String week = QDateUtils.weeks()[calendar.get(Calendar.DAY_OF_WEEK) - 1];
            chargerEnergyRes.setWeek(week);
            chargerEnergyRes.setTime(searchTimes.get(m));
            weeks.add(week);
            list.add(chargerEnergyRes);
        }
        result.put("list", list);
        result.put("weeks", weeks);
        result.put("times", QDateUtils.getTime());
        return result;
    }
}
